<template v-if="visible">
    <a-modal v-model:open="visible" :title="title" @ok="handleOk">
        <a-form
            ref="formRef"
            :model="formState"
            :rules="rules"
            :label-col="{ span: 6 }"
            autocomplete="off"
            v-if="visible"
        >
            <a-form-item label="名字" name="name">
                <a-input v-model:value="formState.name" />
            </a-form-item>
            <a-form-item label="销量" name="sales">
                <a-input-number
                    v-model:value="formState.sales"
                    style="width: 100%"
                    addon-after="万元"
                />
            </a-form-item>
            <a-form-item label="销售渠道" name="salesChannel">
                <a-select v-model:value="formState.salesChannel" :options="channelOptions" />
            </a-form-item>
            <a-form-item label="产品类别" name="productType">
                <a-select v-model:value="formState.productType" :options="productTypeOptions" />
            </a-form-item>
            <a-form-item label="回购量" name="repurchaseCounts">
                <a-input-number
                    v-model:value="formState.repurchaseCounts"
                    style="width: 100%"
                    addon-after="万元"
                />
            </a-form-item>
            <a-form-item label="产量" name="products">
                <a-input-number
                    v-model:value="formState.products"
                    style="width: 100%"
                    addon-after="吨"
                />
            </a-form-item>
            <a-form-item label="出货量" name="outProducts">
                <a-input-number
                    v-model:value="formState.outProducts"
                    style="width: 100%"
                    addon-after="吨"
                />
            </a-form-item>
            <a-form-item label="库存量" name="stock">
                <a-input-number
                    v-model:value="formState.stock"
                    style="width: 100%"
                    addon-after="吨"
                />
            </a-form-item>
            <a-form-item label="今日价格" name="todayPrice">
                <a-input-number
                    v-model:value="formState.todayPrice"
                    style="width: 100%"
                    addon-after="元/斤"
                />
            </a-form-item>
            <a-form-item label="昨日价格" name="yestodayPrice">
                <a-input-number
                    v-model:value="formState.yestodayPrice"
                    style="width: 100%"
                    addon-after="元/斤"
                />
            </a-form-item>
            <a-form-item label="未来价格预测" name="futurePrice">
                <a-input-number
                    v-model:value="formState.futurePrice"
                    style="width: 100%"
                    addon-after="元/斤"
                />
            </a-form-item>
        </a-form>
    </a-modal>
</template>

<script lang="ts">
import { defineComponent, reactive, toRefs, ref } from 'vue'
import type { FormInstance } from 'ant-design-vue'
import type { Ref } from 'vue'
import Lodash from 'lodash'

import { toast } from '@/utils/common'
import { Edit } from '../../store'
import { channelOptions, productTypeOptions } from '../../constant'
import type { ItemResponse } from '../../type'
const defaultFormState = {
    id: '',
    name: '',
    sales: '',
    salesChannel: '',
    repurchaseCounts: '',
    products: '',
    outProducts: '',
    stock: '',
    todayPrice: '',
    yestodayPrice: '',
    futurePrice: '',
    createdTime: '',
    updatedTime: ''
}
export default defineComponent({
    name: 'EditDialog',
    emits: ['refresh'],

    setup(props, { emit }) {
        const state = reactive({
            visible: false,
            title: '',
            formState: JSON.parse(JSON.stringify(defaultFormState))
        })

        const constants = {
            rules: {} as any,
            channelOptions,
            productTypeOptions
        }
        for (let key in defaultFormState) {
            constants.rules[key] = {
                required: true,
                message: '请输入！',
                trigger: 'blur'
            }
        }
        const components = {
            formRef: ref<FormInstance>()
        } as {
            formRef: Ref<FormInstance>
        }
        const methods = {
            open(row?: ItemResponse) {
                state.title = row?.id ? '编辑' : '新增'
                if (row) {
                    state.formState = Lodash.cloneDeep(row)
                } else {
                    state.formState = JSON.parse(JSON.stringify(defaultFormState))
                }
                state.visible = true
            },
            async handleOk() {
                const isOK = await components.formRef.value.validateFields()
                if (!isOK) return
                const res: any = await Edit(state.formState)
                if (res.status === 201) {
                    toast('err', '', res.result.msg)
                    return
                }
                emit('refresh')
                state.visible = false
                toast()
            }
        }
        return {
            ...toRefs(state),
            ...methods,
            ...constants,
            ...components
        }
    }
})
</script>
